package com.aloda.ojsandbox.pojo.enums;

import cn.hutool.core.util.StrUtil;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author aloda
 * @Date 2025/5/1 14:13
 */
public enum CodeSandboxLanguageEnum {

    C_PLUS_PLUS("c++", 1),
    JAVA("java", 2),
    PYTHON("python", 3);

    private final String text;
    private final Integer value;

    CodeSandboxLanguageEnum(String text, Integer value) {
        this.text = text;
        this.value = value;
    }

    /**
     * 获取值列表
     *
     * @return
     */
    public static List<Integer> getValues() {
        return Arrays.stream(values()).map(item -> item.value).collect(Collectors.toList());
    }

    /**
     * 根据 value 获取枚举
     *
     * @param value
     * @return
     */
    public static CodeSandboxLanguageEnum getEnumByValue(Integer value) {
        if (ObjectUtils.isEmpty(value)) {
            return null;
        }
        for (CodeSandboxLanguageEnum anEnum : CodeSandboxLanguageEnum.values()) {
            if (anEnum.value.equals(value)) {
                return anEnum;
            }
        }
        return null;
    }

    public static CodeSandboxLanguageEnum getEnumByText(String text) {
        if (StrUtil.isEmpty(text)) {
            return null;
        }
        for (CodeSandboxLanguageEnum anEnum : CodeSandboxLanguageEnum.values()) {
            if (anEnum.text.equals(text)) {
                return anEnum;
            }
        }
        return null;
    }

    public Integer getValue() {
        return value;
    }

    public String getText() {
        return text;
    }
}
